﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace RabbitMQ示例
{
    /// <summary>
    /// RabbitMQ配置文件
    /// </summary>
    public class RabbitMQConfig
    {
        private static readonly RabbitMQConfig instance = new RabbitMQConfig();

        /// <summary>
        /// 创建一个配置文件读取的单例
        /// </summary>
        public RabbitMQConfig()
        {
            Init();
        }

        private MqConfig conf = null;

        /// <summary>
        /// RabbitMQ配置文件
        /// </summary>
        public MqConfig Conf
        {
            get { return conf; }
        }
        /// <summary>
        /// RabbitMQ一个配置文件读取的单例
        /// </summary>
        public static RabbitMQConfig Instance
        {
            get { return instance; }
        }

        public void Init()
        {
            //默认运行目录
            string defaultPath = "/IniFiles/RabbitMQConfig.xml";
            string fullFilePath = AppDomain.CurrentDomain.BaseDirectory + defaultPath;

            //创建一个临时对像,不然用下面的    ExeConfigurationFileMap 读取文件不成功
            if (!System.IO.File.Exists(fullFilePath))
            {
                throw new System.IO.FileNotFoundException(string.Format("末在iniFiles文件夹中,找到RabbitMQ配置文件:{0}", fullFilePath));
            }

            try
            {
                string xmlcontent = string.Empty;
                xmlcontent = System.IO.File.ReadAllText(fullFilePath);
                //检查内容是否为空
                if (!string.IsNullOrEmpty(xmlcontent))
                {
                    System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(MqConfig));
                    System.IO.StringReader sw = new System.IO.StringReader(xmlcontent);
                    //序列化功能
                    conf = (MqConfig)ser.Deserialize(sw);
                }
                else
                {
                    throw new System.IO.FileNotFoundException(string.Format("RabbitMQ配置文件:{0},中的文件内容这空请检查文件:{1}", fullFilePath));
                }

            }
            catch (Exception ex)
            {
                throw new System.IO.FileNotFoundException(string.Format("RabbitMQ配置文件:{0},中的文件内容无法正常反序列化:{1}", fullFilePath, ex));
            }





        }
    }


    #region MQ配置
    /// <summary>
    /// MQ主要配置信息
    /// </summary>
    public class MqConfig
    {
        /// <summary>
        /// 主机名 多个用,分开
        /// </summary>
        public string HostName { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// 虚拟机路径
        /// </summary>
        public string VirtualHost { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }


        /// <summary>
        /// MQ端口默认5672
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// 超过此总数后自动写磁盘
        /// </summary>
        private int outOfCountToDisk = 20000;
        /// <summary>
        /// 超过此总数后自动写磁盘
        /// </summary>
        public int OutOfCountToDisk
        {
            get { return outOfCountToDisk; }
            set { outOfCountToDisk = value; }
        }

        /// <summary>
        /// 队列集合
        /// </summary>
        public List<MQQueueInfo> QueueCollection { get; set; }
        /// <summary>
        /// 实时数据序列化到MQ的线程对像数  如果大于1则可能会导致一个终端的数据顺序变乱-
        /// </summary>
        private byte convertThread = 1;
        /// <summary>
        /// 实时数据序列化到MQ的线程对像数  如果大于1则可能会导致一个终端的数据顺序变乱-
        /// </summary>
        public byte ConvertThread
        {
            get { return convertThread; }
            set { convertThread = value; }
        }


    }
    /// <summary>
    /// 队列秘交换机信息
    /// </summary>
    public class MQQueueInfo
    {
        /// <summary>
        /// 交换机名
        /// </summary>
        public string ExchangeName { get; set; }
        /// <summary>
        /// 交换机类型
        /// </summary>
        public string ExchangeType { get; set; }

        /// <summary>
        /// 队列名
        /// </summary>
        public string QueueName { get; set; }

        /// <summary>
        /// 数据投递模式1,非持久化,2持久化
        /// </summary>
        public int DeliveryMode { get; set; }

        /// <summary>
        /// 是否需要应答,默认不需要
        /// </summary>
        public bool NeedAck { get; set; }

        /// <summary>
        /// 是否需要持久化
        /// </summary>
        public bool Durable { get; set; }

        /// <summary>
        /// 路由Key
        /// </summary>
        public String RoutingKey { get; set; }
    }
    #endregion
}
